package srst.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.deeplearning4j.models.word2vec.Word2Vec;

import srst.ai.CosinSimilarityUtil;
import srst.ai.SegmentWordUtil;
import srst.ai.Word2vecUtil;
import srst.ai.gephi.ModularityService;
import srst.util.FilePathUtil;
import srst.util.FileUtil;
import srst.util.PDFHandleUtil.PDFReaderUtil;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间：2017年5月18日 上午9:28:47 
* @version 1.0  
*/

public class SrstService {
	
	private KeywordService keywordService;
	private ModularityService modularityService;
	
	private List<String> keywordList;
	private List<String> resumeList; 
	private Map<String, String> resumeMap= new HashMap<>();
	
	
	
	private List<String> getKeywordsList() throws IOException{
		return keywordService.getKeywordList(FilePathUtil.KEYWORDS_PATH);
	}
	
	
	/**
	 * @throws IOException  
	* @author  作者 : YUHU YUAN
	* @date 创建时间：2017年5月18日 上午9:34:48 
	* @parameter filePath 得到存放简历的文件夹路径  
	* D:\\huawei_hackathon\\resume\\
	* D:\huawei_hackathon\sign_resume\\
	* @return
	* @throws 
	*/
	private List<String> getResumeList() throws IOException{
		List<String> resumeList = getResumeList(FilePathUtil.RESUME_PATH);
		List<String> signList = getResumeList(FilePathUtil.SIGN_PATH);
		resumeList.addAll(signList);
		return resumeList;
	}
	
	private List<String> getResumeList(String path) throws IOException{
		File resumeFolder = new File(FilePathUtil.RESUME_PATH);
		File[] fileArray = resumeFolder.listFiles();
		List<String> resumeList = new ArrayList<>();
		for(File resume : fileArray){
			String resumeText = PDFReaderUtil.getTextFromPDF(resume.getAbsolutePath()).replaceAll("[(\\s)|( )|( )]+", " ");
			String resumeName = resume.getName();
			resumeName = resumeName.substring(0, resumeName.indexOf("."));
			resumeMap.put(resumeName, resumeText);
			resumeList.add(resumeText);
		}
		return resumeList;
	}
	
	
	
	
	
	/** 
	* @author  作者 : YUHU YUAN
	* @date 创建时间：2017年5月18日 上午9:50:44 
	* @parameter 
	* @return
	* @throws
	* 得到分词后的结果，这里主要是提供给word2vec使用，路径在导出工程之后还是要改
	* 
	*/
	public void segmentedResume2File() throws IOException{
		List<String> segmentedResume =  SegmentWordUtil.generateSegmentedWordResultByInitSeed(resumeList, keywordList);
		FileUtil.write2File(segmentedResume,FilePathUtil.SEGMENT_RESUME_PATH);
	}
	
	public Word2Vec getWord2vecModelAndWordVector() throws IOException{
		return Word2vecUtil.getWord2Vec(FilePathUtil.SEGMENT_RESUME_PATH,FilePathUtil.WORD_EMBEDDING_VECTOR_PATH);
	}
	
	
	/** 
	* @author  作者 : YUHU YUAN
	* @date 创建时间：2017年5月18日 下午2:50:15 
	* @parameter 
	* @return
	* @throws
	* 计算它的相似性，目前先不使用它来计算词与词之间的相似性，先直接从Word2vec中取出来
	* 后续的工作如果需要再用它进行计算。
	*/
	public void computeCosSim(){
		try {
			CosinSimilarityUtil.computeCosSim2Text(FilePathUtil.WORD_EMBEDDING_VECTOR_PATH, FilePathUtil.COSINSIMILARITY_PATH);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/** 
	* @author  作者 : YUHU YUAN
	* @date 创建时间：2017年5月18日 下午2:55:57 
	* @parameter 
	* @return
	 * @throws IOException 
	* @throws
	* 得到每个关键词的最近似的5个其他词，这样使用的信息更加全面一些
	* 
	*/
	public List<String> getSimilarWords(Word2Vec word2Vec,int numWord) throws IOException{
		List<String> keywords = new ArrayList<>();
		Set<String> keywordSet = new HashSet<>();
		
		for(String word : keywordList){
			Collection<String> lst = word2Vec.wordsNearest(word, numWord);
			keywordSet.addAll(lst);
		}
		keywordSet.addAll(keywordList);
		keywords.addAll(keywordSet);
		System.out.println(keywords.size());
		return keywords;
	}
	
	
	
	public void constructTextVectors2File(Word2Vec word2Vec, List<String> extendKeyWords, String outputPath) throws IOException{
		BufferedWriter writer = FileUtil.getWriter(outputPath);
		StringBuffer temp = new StringBuffer();
		for(String resumeName : resumeMap.keySet()){
			temp.append(resumeName).append(" ");
			String resumeText = resumeMap.get(resumeName);
			for(String word : extendKeyWords){
				if(resumeText.contains(word)){
					double[] vector = word2Vec.getWordVector(word);
					if (vector != null) {
						for (int i = 0; i < vector.length; ++i) {
							temp.append(vector[i]).append(" ");
						}
						continue;
					}
				}
				for(int i=0; i<100; ++i){
					temp.append(0).append(" ");
				}
			}
			
			String graphInfo = temp.toString();
			writer.write(graphInfo);
			writer.newLine();
			String[] test = temp.toString().split(" ");
			System.out.println(test.length);
			writer.flush();
			temp.setLength(0);
		}
		
		if(writer!=null){
			writer.close();
		}
		
	}
	
	
	
	public void function() throws IOException{
		keywordList = getKeywordsList();
		resumeList = getResumeList();
		
		segmentedResume2File();
		
		Word2Vec word2Vec =  getWord2vecModelAndWordVector();
		
		List<String> extendKeyWords =  getSimilarWords(word2Vec,5);
		for(String word : extendKeyWords){
			System.out.println("keyword: "+word);
		}
		
		constructTextVectors2File(word2Vec, extendKeyWords,FilePathUtil.RESUME_VECTOR_PATH);
		
		List<String> computeResult = CosinSimilarityUtil.computeCosSim2Text(FilePathUtil.RESUME_VECTOR_PATH, FilePathUtil.COSINSIMILARITY_PATH);
		
		modularityService.computeUndirectedGraphModularity(computeResult,resumeMap);
		
		modularityService.resultHandle();
		
	}
	
	
	public KeywordService getKeywordService() {
		return keywordService;
	}


	public void setKeywordService(KeywordService keywordService) {
		this.keywordService = keywordService;
	}


	public ModularityService getModularityService() {
		return modularityService;
	}


	public void setModularityService(ModularityService modularityService) {
		this.modularityService = modularityService;
	}


//	public static void main(String[] args) throws IOException{
//		SrstService service = new SrstService();
//		service.function();
//	}
	
	
	
	

}


















